home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C++ / Libraries / grayimage / vimage_io.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-30  |  2.5 KB  |  107 lines  |  [TEXT/R*ch]

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /*
  3.  ************************************************************************
  4.  *
  5.  *               Grayscale Image
  6.  *
  7.  *          Verify reading/writing of various image formats
  8.  *
  9.  * $Id: vimage_io.cc,v 1.1 1994/02/07 15:50:58 oleg Exp oleg $
  10.  *
  11.  ************************************************************************
  12.  */
  13.  
  14. #include "image.h"
  15. #include <builtin.h>
  16. #include <ostream.h>
  17.  
  18. static IMAGE Test_image(16,32,8); //(256,512,8);
  19.  
  20.                     // Verify the pixels have the value
  21.                     // that is expected
  22. static void verify_pixel_value(const GRAY val)
  23. {
  24.   register int i,j;
  25.   for(i=0; i<Test_image.q_nrows(); i++)
  26.     for(j=0; j<Test_image.q_ncols(); j++)
  27.       if( Test_image(i,j) != val )
  28.     _error("Pixel [%d,%d] has the value 0x%x different from expected 0x%x",
  29.            i,j,Test_image(i,j),val);
  30. }
  31.  
  32. static char * Image_file_name = "/tmp/aa";
  33.  
  34.                 // Write different kinds of image file formats
  35.                 // into the file named Image_file_name
  36. static void write_xwd(const IMAGE& image)
  37. {
  38.   image.write_xwd(Image_file_name);
  39. }
  40.  
  41. static void write_pgm(const IMAGE& image)
  42. {
  43.   image.write_pgm(Image_file_name);
  44. }
  45.  
  46. static void write_tiff(const IMAGE& image)
  47. {
  48.   image.write_tiff(Image_file_name);
  49. }
  50.  
  51. static void write_default(const IMAGE& image)
  52. {
  53.   image.write(Image_file_name);
  54. }
  55.  
  56.  
  57.                 // Check reading and writing of a test image
  58. static void test_image_io(void (*writer)(const IMAGE& image))
  59. {
  60.   cout << "\n\n\tWriting the Test_image and reading it back\n";
  61.   Test_image = 154; Test_image(0,0) = 0; Test_image(1,0) = 1;
  62.   writer(Test_image);
  63.   IMAGE read_back(Image_file_name,1);
  64.   Test_image -= read_back;
  65.   verify_pixel_value(0);
  66.  
  67.   cout << "\nDone\n";
  68. }
  69.  
  70.                 // Test the I/O using a real image
  71. static void real_image_io(const IMAGE& image, 
  72.               void (*writer)(const IMAGE& image))
  73. {
  74.   cout << "\n\n\tWriting a real image and reading it back\n";
  75.   
  76.   start_timer();
  77.   writer(image);
  78.   cout << "\nIt took " << return_elapsed_time(0) 
  79.        << " sec to write the image\n";
  80.  
  81.   start_timer();
  82.   IMAGE read_again_image(Image_file_name);
  83.   cout << "\nIt took " << return_elapsed_time(0) 
  84.       << " sec to read the image\n";
  85.   assert( read_again_image == image );
  86.  
  87.   cout << "\nDone\n";
  88. }
  89.  
  90.  
  91.  
  92. main(void)
  93. {
  94.   cout << "\n\nTest image input/output operations\n";
  95.  
  96.   test_image_io(write_xwd);
  97.   test_image_io(write_pgm);
  98.   test_image_io(write_tiff);
  99.   test_image_io(write_default);
  100.  
  101.   IMAGE image("pictures/512.xwd");
  102.   real_image_io(image,write_xwd);
  103.   real_image_io(image,write_pgm);
  104.   real_image_io(image,write_tiff);
  105.   real_image_io(image,write_default);
  106. }
  107.